package com.zwf.service.impl;

import com.zwf.constant.StatusConstant;
import com.zwf.entity.Orders;
import com.zwf.mapper.*;
import com.zwf.service.WorkSpaceService;
import com.zwf.vo.BusinessDataVO;
import com.zwf.vo.DishOverViewVO;
import com.zwf.vo.OrderOverViewVO;
import com.zwf.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author zwf
 * @date 2024/2/21 13:31
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 查询指定时间段运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {

        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //新增用户数
        Integer newUsers=userMapper.countByMap(map);

        //今日总订单数
        Integer allOrders = orderMapper.countByMap(map);

        //今日有效订单数
        map.put("status",5);
        Integer validOrders = orderMapper.countByMap(map);

        //订单完成率
        Double orderCompletionRate=0.0;
        if(allOrders!=0){
            orderCompletionRate=validOrders.doubleValue()/allOrders;
        }

        //营业额
        Double turnover=orderMapper.sumTurnoverByMap(map);
        turnover=turnover==null?0.0:turnover;

        //平均客单价
        Double unitPrice=0.0;
        if(validOrders!=0){
            unitPrice=turnover/validOrders;
        }

        //封装返回结果
        BusinessDataVO businessDataVO=BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrders)
                .build();

        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {

        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));

        //全部订单
        Integer allOrders=orderMapper.countByMap(map);

        //已取消订单
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //已完成订单
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //待派送数量
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //待接单数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        //封装返回结果
        OrderOverViewVO orderOverViewVO=OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {

        //已停售菜品数量
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        
        //已起售菜品数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);

        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();

        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {

        //已停售套餐
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);

        //已起售套餐
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);

        SetmealOverViewVO setmealOverViewVO=SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
        return setmealOverViewVO;
    }
}
